import zipfile
import os


# todo 解压文件
def upzip(filePath):
    """
    解压zip文件到其同级目录下
    :param filePath: 文件目录
    :return:
    """
    # TODO 解压

    unzip_target = os.path.dirname(filePath)
    print(filePath)
    print(unzip_target)
    with zipfile.ZipFile(filePath) as zf:
        zf.extractall(path=unzip_target)
        pass

    # todo 解决文件名中文乱码
    try:
        for dirpath, dirnames, filenames in os.walk(unzip_target, topdown=False):
            print("dirpath : " + dirpath)
            #  文件夹重命名
            for subfolder in dirnames:
                src = os.path.join(dirpath, subfolder)
                dst = os.path.join(dirpath, subfolder.encode("cp437").decode("gbk"))
                os.rename(src, dst)
                print("\t subfolder :", src + " -> " + dst)
            # 文件重命名
            for filename in filenames:
                src = os.path.join(dirpath, filename)
                dst = os.path.join(dirpath, filename.encode("cp437").decode("gbk"))
                os.rename(src, dst)
                print("\t filename :", src + " -> " + dst)
        pass
    except BaseException as exc:
        print("不知道怎么解决反正功能完成了顺便报个错")
        print(exc)
    pass


if __name__ == "__main__":
    filePath = "关于我转生成为史莱姆这档事.zip"
    upzip(filePath)
    pass
